package com.geely.club.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by mengyangyang on 2015-10-15.
 */
public class TimeUtil {

    /**
     * 时间显示策略：今天的1小时内的邮件采用X分钟前，
     * 今天的1小时外的用几点几分（24小时制），
     * 超过1天小于2天的采用昨天几点几分（24小时制），
     * 超过2天小于3天的，采用前天几点几分，
     * 超出3天的邮件采用几月几号即可
     *
     * @return
     */
    public static String getFormatedTimeStr(long time) {
        date.setTime(time);
        if (isToday(time)) {
            return formater1.format(date).toString();
        } else if (isYestaday(time)) {
            return formater2.format(date).toString();
        } else if (isTheDayBeforeYestaday(time)) {
            return formater3.format(date).toString();
        } else if (isThisYear(time)) {
            return formater4.format(date).toString();
        } else {
            return formater5.format(date).toString();
        }
    }

    public static String getFormatedTimeStr(Date date) {
        long time = date.getTime();
        if (isToday(time)) {
            return formater1.format(date).toString();
        } else if (isYestaday(time)) {
            return formater2.format(date).toString();
        } else if (isTheDayBeforeYestaday(time)) {
            return formater3.format(date).toString();
        } else if (isThisYear(time)) {
            return formater4.format(date).toString();
        } else {
            return formater5.format(date).toString();
        }
    }

    public static String getFormatedTalkTimeStr(long time) {
        date.setTime(time);
        if (isToday(time)) {
            return formater6.format(date).toString();
        } else if (isYestaday(time)) {
            return formater7.format(date).toString();
        } else if (isTheDayBeforeYestaday(time)) {
            return formater8.format(date).toString();
        } else if (isThisYear(time)) {
            return formater9.format(date).toString();
        } else {
            return formater10.format(date).toString();
        }
    }

    private static Date date = new Date();
    private static SimpleDateFormat formater1 = new SimpleDateFormat("HH点mm分");
    private static SimpleDateFormat formater2 = new SimpleDateFormat("昨天  HH点mm分");
    private static SimpleDateFormat formater3 = new SimpleDateFormat("前天  HH点mm分");
    private static SimpleDateFormat formater4 = new SimpleDateFormat("MM月dd号");
    private static SimpleDateFormat formater5 = new SimpleDateFormat("yyyy年MM月dd号");

    private static SimpleDateFormat formater6 = new SimpleDateFormat("HH:mm");
    private static SimpleDateFormat formater7 = new SimpleDateFormat("昨天  HH:mm");
    private static SimpleDateFormat formater8 = new SimpleDateFormat("前天  HH:mm");
    private static SimpleDateFormat formater9 = new SimpleDateFormat("MM-dd HH:mm");
    private static SimpleDateFormat formater10 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    final static public boolean isToday(long time) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.set(Calendar.HOUR_OF_DAY, 0);
        return time > c.getTimeInMillis();
    }

    final static public boolean isYestaday(long time) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.set(Calendar.HOUR_OF_DAY, 0);
        long today = c.getTimeInMillis();
        long yestoday = today - 60 * 60 * 24 * 1000;
        return time < today && time > yestoday;
    }

    final public static boolean isTheDayBeforeYestaday(long time) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.set(Calendar.HOUR_OF_DAY, 0);
        long yestoday = c.getTimeInMillis() - 60 * 60 * 24 * 1000;
        long theDayBeforeYestaday = yestoday - 60 * 60 * 24 * 1000;
        return time < theDayBeforeYestaday && time > yestoday;
    }

    final public static boolean isThisYear(long time) {
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        return time > c.getTimeInMillis();
    }

    final public static int isWhichWeekday(int i) {
        int w = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
        int week = w;
        switch (i) {
            case 0:
                week = w - 3;
                if (week < 0) {
                    week = week + 7;
                }
                break;
            case 1:
                week = w - 2;
                if (week < 0) {
                    week = week + 7;
                }
                break;
            case 2:
                week = w - 1;
                if (week < 0) {
                    week = week + 7;
                }
                break;
            case 3:
                week = w;
                break;
            case 4:
                week = w + 1;
                if (week > 6) {
                    week = week - 7;
                }
                break;
            case 5:
                week = w + 2;
                if (week > 6) {
                    week = week - 7;
                }
                break;
            case 6:
                week = w + 3;
                if (week > 6) {
                    week = week - 7;
                }
                break;
        }
        return week;
    }
    final public static String weekdayFormater(int i){
        switch (i){
            case 0:
                return "日";
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";

        }
        return "";
    }

    final public static int isWhichDay(int i){
        long today = Calendar.getInstance().getTimeInMillis();
        long day = today + 60 * 60 * 24 * 1000 * i;
        Calendar c = Calendar.getInstance(Locale.CHINA);
        c.setTimeInMillis(day);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    final public static Date getDate(int i){
        long today = Calendar.getInstance().getTimeInMillis();
        long day = today + 60 * 60 * 24 * 1000 * i;
        date.setTime(day);
        return date;
    }

}
